package com.seeway.wms.utils

import android.util.Log
import com.geen.tuoke.utils.Arith
import java.math.BigDecimal
import java.math.RoundingMode
import java.util.regex.Pattern

object BigDecimalUtils {
    // 需要精确至小数点后几位
    private const val DECIMAL_POINT_NUMBER: Int = 1

    @JvmStatic
    fun formatText(numberFloat: Double?): String {

        if (numberFloat == null || numberFloat == 0.0) {
            return BigDecimal.valueOf(0).stripTrailingZeros().toPlainString()
        }

        val resultNumber = Arith.round(numberFloat, 4)
        return BigDecimal.valueOf(resultNumber).stripTrailingZeros().toPlainString()
    }

    @JvmStatic
    fun formatTextPrice(numberFloat: Double?): String {
        if (numberFloat == null) {
            return ""
        }
        if (numberFloat == 0.0) {
            return BigDecimal.valueOf(0).stripTrailingZeros().toPlainString()
        }
        val resultNumber = Arith.round(numberFloat, 4)
        return BigDecimal.valueOf(resultNumber).stripTrailingZeros().toPlainString()
    }


    @JvmStatic
    fun add(d1: Double, d2: Double): Double = add(d1, d2, DECIMAL_POINT_NUMBER)

    @JvmStatic
    fun add(d1: Double, d2: Double, newScale: Int): Double =
        BigDecimal("$d1").add(BigDecimal("$d2")).setScale(newScale, BigDecimal.ROUND_HALF_DOWN).toDouble()

    @JvmStatic
    fun sub(d1: Double, d2: Double): Double = sub(d1, d2, DECIMAL_POINT_NUMBER)


    @JvmStatic
    fun sub(d1: Double, d2: Double, newScale: Int): Double =
        BigDecimal("$d1").subtract(BigDecimal("$d2")).setScale(newScale, BigDecimal.ROUND_HALF_DOWN).toDouble()


    @JvmStatic
    fun mul(d1: Double, d2: Double): Double = mul(d1, d2, DECIMAL_POINT_NUMBER)

    @JvmStatic
    fun mul(d1: Double, d2: Double, newScale: Int): Double =
        BigDecimal("$d1").multiply(BigDecimal("$d2")).setScale(newScale, BigDecimal.ROUND_HALF_DOWN).toDouble()


    @JvmStatic
    fun div(d1: Double, d2: Double): Double = div(d1, d2, DECIMAL_POINT_NUMBER)

    @JvmStatic
    fun div(d1: Double, d2: Double, newScale: Int): Double =
        BigDecimal("$d1").divide(BigDecimal("$d2"), newScale, RoundingMode.HALF_UP).toDouble()


    @JvmStatic
    fun remainder(d1: Double, d2: Double): Double {
        return BigDecimal("$d1").divideAndRemainder(BigDecimal("$d2"))[1].toDouble()

    }


    @JvmStatic
    fun formatMateDouble(d1: Double?): String {
        if (d1 == null) return ""
        if (d1 == 0.0) return "0"
        val doubleStr = BigDecimal(d1.toString()).toPlainString()
        return try {
            if (doubleStr.endsWith(".0")) {
                doubleStr.substring(0, doubleStr.length - 2)
            } else {
                doubleStr
            }
        } catch (e: Exception) {
            doubleStr
        }

    }

    @JvmStatic
    fun formatMateDouble(d1: String?): String {
        if (d1 == null) return ""
        return if (d1.toString().endsWith(".0")) {
            d1.substring(0, d1.length - 2)
        } else if (d1.toString().endsWith(".0000")) {
            d1.substring(0, d1.length - 5)
        } else {
            d1
        }
    }

    @JvmStatic
    fun formatMateDoublePreventNegative(d1: Double?): String {
        if (d1 == null) return ""
        if (d1 < 0.0) return "0"
        return formatMateDouble(d1)


    }

}